package cn.liontalk.concurrent.itsoku.current;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPoolDemo {

//    public static void main(String[] args) {
//        ExecutorService threadPoolExecutor = Executors.newCachedThreadPool();
//        ExecutorService executorService = null;
//        ThreadPoolExecutor executor ;
//        ScheduledExecutorService sh = null;
//        for (int i = 0; i <= 50; i++) {
//            String taskName = "任务" + i;
//            threadPoolExecutor.execute(() -> {
//                System.out.println(Thread.currentThread().getName() + "处理" + taskName);
//                //模拟任务内部处理耗时
//                try {
//                    TimeUnit.SECONDS.sleep(991);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
//        }
//        threadPoolExecutor.shutdown();
//    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(System.currentTimeMillis());
        //任务执行计数器



        AtomicInteger count = new AtomicInteger(1);
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            int currCount = count.getAndIncrement();
            System.out.println(Thread.currentThread().getName());
            System.out.println(System.currentTimeMillis() + "第" + currCount + "次" + "开始执行");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(System.currentTimeMillis() + "第" + currCount + "次" + "执行结束");
        }, 1, 1, TimeUnit.SECONDS);
    }
}
